package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.LoadGoodsType;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.GoodsTypeService;
import com.atguigu.jxc.service.LogService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

/**
 * @author dyqstart
 * @create 2021-09-03-19:56
 * @since 1.0.0
 */
@RestController
@RequestMapping("/goodsType")
public class GoodsTypeController {

    @Autowired
    GoodsTypeService goodsTypeService;

    @Autowired
    LogService logService;

    /**
     * 加载所有分类信息
     * @return
     */
    @PostMapping("/loadGoodsType")
    public String loadGoodsType(){
        List<LoadGoodsType> list = goodsTypeService.loadGoodsType();
        Gson gson = new Gson();
        String string = gson.toJson(list);
        Log log = new Log(Log.SELECT_ACTION,"加载所有分类信息");
        logService.save(log);
        return string;
    }

    /**
     * 新增分类
     * @param goodsTypeName 分类名
     * @param pId 分类id
     * @return
     */
    @PostMapping("/save")
    public ServiceVO saveGoodsType(String goodsTypeName,Integer pId){
        goodsTypeService.saveGoodsType(goodsTypeName,pId);
        loadGoodsType();
        Log log = new Log(Log.INSERT_ACTION,"新增分类");
        logService.save(log);
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 删除分类
     * @param goodsTypeId
     * @return
     */
    @PostMapping("/delete")
    public ServiceVO deleteGoodsType(Integer goodsTypeId){
        goodsTypeService.deleteGoodsType(goodsTypeId);
        loadGoodsType();
        Log log = new Log(Log.DELETE_ACTION,"删除分类");
        logService.save(log);
        return new ServiceVO(100,"请求成功",null);
    }


}
